<?php

class WebglController extends Controller {
	public function __construct($id, $module = null) {
		parent::__construct($id, $module);
		$this -> layout = 'application.views.layouts.layout';
	}

	public function actionIndex() {
		$this -> render('index');
	}
	
	public function actionStudy() {
		$this -> render('study');
	}
	
	public function actionhome() {
		$this -> layout = 'application.views.layouts.layoutHome';
		$this -> render('home');
	}
	
	
	public function actionBuild(){
		$this -> render('buildHouse');
	}

	public function actionFacade(){
		$houseStyle=$_REQUEST['houseStyle'];
		$this -> render('facadeHouse',array('houseStyle'=>$houseStyle));
	}
	
	public function actionFacadeTool(){
		
		// echo json_decode($_REQUEST['walls']);
		foreach($_REQUEST['walls'] as $rs) {
			
		}
		$this -> renderPartial('facadeToolbar');
	}
	
	
	public function actionPanel(){
		$this -> renderPartial('panel');
	}
	
	public function actionDelWall(){
		echo 'dd';
	}
	public function actionFacility() {
		$this -> layout = 'application.views.layouts.none';
				$pageNo = 1;
		$pageSize=10;
		if(isset($_REQUEST['pageNo'])) {
			$pageNo = $_REQUEST['pageNo'];
		}
		if(isset($_REQUEST['pageSize'])){
			$pageSize=$_REQUEST['pageSize'];
		}
		
		$facilities = GoodsGeometry::model() -> getAllFetchPage($pageNo,$pageSize);
		// echo json_encode($facilities);
		$this -> render('facility',array('facilities'=>$facilities));
		
		// $pageNo = 1;
		// if(isset($_REQUEST['pageNo'])) {
			// $pageNo = $_REQUEST['pageNo'];
		// }
		// if(isset($_REQUEST['dependId'])) {
			// $goodsGeo = GoodsGeometry::model() -> getFindBygTypeIdAndDependIdOnPage($_REQUEST['gTypeId'], $_REQUEST['dependId'], $pageNo, 10);
		// } else {
			// $goodsGeo = GoodsGeometry::model() -> getFindBygTypeIdOnPage($_REQUEST['gTypeId'], $pageNo, 10);
		// }
// 
		// echo json_encode($goodsGeo);
		// // echo json_encode($_REQUEST ['gTypeId'] );
		// $this -> render('facility', array('goGeo' => $goodsGeo));
		// // $gTypeId=$_POST ['gTypeId'];
		
		
	}
	
	public function actionShowFacilityDetail(){
		$geometry=$_REQUEST['geometry'];
		$goodsName=$_REQUEST['goodsName'];
		$description=$_REQUEST['description'];
		$this -> render('showFacilityDetail',array('geometry'=>$geometry,"goodsName"=>$goodsName,"description"=>$description));
	}
	public function actionShowFacilityThumb(){
		$pageNo = 1;
		$pageSize=10;
		if(isset($_REQUEST['pageNo'])) {
			$pageNo = $_REQUEST['pageNo'];
		}
		if(isset($_REQUEST['pageSize'])){
			$pageSize=$_REQUEST['pageSize'];
		}
		// if(isset($_REQUEST['dependId'])) {
			// $goodsGeo = GoodsGeometry::model() -> getFindBygTypeIdAndDependIdOnPage($_REQUEST['gTypeId'], $_REQUEST['dependId'], $pageNo, 10);
		// } else {
			// $goodsGeo = GoodsGeometry::model() -> getFindBygTypeIdOnPage($_REQUEST['gTypeId'], $pageNo, 10);
		// }
		
		$facilities = GoodsGeometry::model() -> getAllFetchPage($pageNo,$pageSize);
		$this -> render('showFacility',array('facilities'=>$facilities));
	}
	
	public function actionLoadFacility(){
		$pageNo = 1;
		$pageSize=10;
		if(isset($_REQUEST['pageNo'])) {
			$pageNo = $_REQUEST['pageNo'];
		}
		if(isset($_REQUEST['pageSize'])){
			$pageSize=$_REQUEST['pageSize'];
		}
		// if(isset($_REQUEST['dependId'])) {
			// $goodsGeo = GoodsGeometry::model() -> getFindBygTypeIdAndDependIdOnPage($_REQUEST['gTypeId'], $_REQUEST['dependId'], $pageNo, 10);
		// } else {
			// $goodsGeo = GoodsGeometry::model() -> getFindBygTypeIdOnPage($_REQUEST['gTypeId'], $pageNo, 10);
		// }
		
		$goodsGeo = GoodsGeometry::model() -> getAllFetchPage($pageNo,$pageSize);

		echo json_encode($goodsGeo);
	}
	
	public function actionLoad($gtId = 1) {

		$wgGoodsType = WgGoodsType::model() -> findAllObject();
		$this -> render('place', array('house' => null, 'wgGoodsType' => $wgGoodsType));
	}

	public function actionLoad2($gtId = 1) {
		$wgGoodsType = WgGoodsType::model() -> findAllObject();
		$this -> render('place2', array('wgGoodsType' => $wgGoodsType));
	}

	public function actionGallery($type, $sonTypeId = 0) {
		$this -> layout = 'application.views.layouts.none';
		$t = json_decode($type);
		$sonType = $t -> sonTypes;
		// echo json_encode($sonType[$sonTypeId]->gtId);
		$goGeo = GoodsGeometry::model() -> getFindBygTypeId($sonType[$sonTypeId] -> gtId);
		// echo json_encode($goGeo);
		// $this->renderPartial ( 'showcase',array('sonType'=>$sonType));
		$this -> render('gallery', array('goGeo' => $goGeo));
		// $this->render ( 'loadGeometry',array('sonType'=>$sonType));
	}

	public function actionButtons() {
		$this -> renderPartial('popmenu', array('x' => $_REQUEST['x'], 'y' => $_REQUEST['y']));
	}
	
	public function actionStructurePopmenu(){
		
		$hostInfo=json_decode($_REQUEST['hostInfo']);
		// $this -> renderPartial('structurePopmenu',array('hostInfo'=>	$hostInfo));
		$this -> renderPartial('popmenu',array('hostInfo'=>	$hostInfo));
	}
	
	public function actionPopmenu(){
		// var_dump(json_decode($_REQUEST['hostInfo']));
		 $hostInfo=json_decode($_REQUEST['hostInfo']);
		// echo json_encode($hostInfo->parasitizedFace->gTypeId); 
		// if($hostInfo->parasitizedFace!=null){
		// switch($hostInfo->parasitizedFace->gTypeId){
			// case "fwall":
				// $hostInfo->name='北墙';
				// $hostInfo->description='位于北边的墙';
				// break;
			// case "bwall":
				// $hostInfo->name="南墙";
				// $hostInfo->description="位于南边的墙";
				// break;
			// case "lwall":
				// $hostInfo->name="西墙";
				// $hostInfo->description="位于西边的墙";
				// break;
			// case "rwall":
				// $hostInfo->name="东墙";
				// $hostInfo->description="位于东边的墙";
				// break;	
			// case "floor":
				// $hostInfo->name="地板";
				// $hostInfo->description="地板";
				// break;
			// case "ceiling":
				// $hostInfo->name="天花板";
				// $hostInfo->description="天花板";
				// break;											
			// default:
// // 				gtypeId get the typename from WgGoodsType
		// }
		// }else{
// 			
		// }
		if($hostInfo->name=="wall"){
			$url="popmenuStructure";
		}else if ($hostInfo->name== "structure"){
			$url="popmenuStructure";
		}else{
				$url="popmenuCtrl";
		}
		$this -> renderPartial($url,array('hostInfo'=>	$hostInfo));

		// echo $url;
	}

	public function actionddd() {
		$this -> renderPartial('popmenu', array('x' => $_REQUEST['x'], 'y' => $_REQUEST['y']));
	}

	public function actionUploadGeometry() {

		// $this->renderPartial ( 'showcase',array('sonType'=>$sonType));
		// $this->render ( 'showcase',array('sonType'=>$sonType));
		$this -> renderPartial('uploadGeometry');
	}

	public function actionSaveGeometry() {

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax'] === 'geometry-form') {
			echo CActiveForm::validate($model);
			Yii::app() -> end();
		}
		if(isset($_POST['GoodsGeometryForm'])) {
			$model = new GoodsGeometryForm();
			$model -> setAttributes($_POST['GoodsGeometryForm'], false);
			// echo json_encode($_POST ['GoodsGeometryForm']);
			echo json_encode($model -> attributes);
			$model -> save();
			echo "ok";
			// $model->save();

		}

	}

	public function actionDrawHouseStyle() {
		// echo "dddd";
		$this -> renderPartial('drawHouseStyle');
	}

	public function actionSaveHouseStyle() {
		echo json_encode($_REQUEST['houseType']);
		// $this->renderPartial ( 'saveHouseStyle');
		$model = new HouseStyle();
		$hsId = $model -> count() + 1;
		$model -> roomStyles = $_REQUEST['roomStyles'];
		$model -> description = $_REQUEST['description'];
		$model -> houseType = $_REQUEST['houseType'];
		$model -> hsId = $hsId;
		$model -> save();

	}

	public function actionSaveHouse() {

		$hSession = $_REQUEST['hSession'];

		$model = new Houses;
		$model -> hsid = $hSession['hsid'];
		$model -> uid = 1;

		$rSession = $hSession['rooms'];

		$i = 0;
		foreach($rSession as $rs) {

			$model -> rooms[$i] = new Rooms;
			$model -> rooms[$i] -> roomId = $rs['roomId'];
			$model -> rooms[$i] -> roomType = $rs['roomType'];
			$model -> rooms[$i] -> position = $rs['position'];
			$model -> rooms[$i] -> size = $rs['size'];
			if(isset($rs['facilities'])){
				$model -> rooms[$i] -> facilities = $rs['facilities'];
				}
			if(isset($rs['textures'])){
				$model -> rooms[$i] -> textures = $rs['textures'];
				}
			$i++;
		}
		echo json_encode($model);
		$model -> save();


	}

	public function actionLoadHouse() {
		$h = $_POST['h'];
		// // get house style
		$faciStyle = array();
		$hs = HouseStyle::model() -> findById($h['hsid']);
		// echo json_encode($hs['roomStyles']);
		$object = new stdClass();
		$object -> roomStyles = $hs['roomStyles'];
		$wall_textures = array();
		foreach($h['rooms'] as $r) {

			// hostFacInfo={
                // "description" : description,
                // "parasitizedFace" : faciStyle[i].parasitizedFace,
                // "rndId" : faciStyle[i].rndId,
                // "gTypeId":gTypeId,
                // "name":name,
                // "roomId":roomId
            // };
            			//对墙纸的处理
			if($r['textures']!="null"){
				foreach($r['textures'] as $t) {
					$texture = new stdClass();
					$texture->wallType=$t['wallType'];
					$texture->textureUrl=$t['textureUrl'];
					$texture->roomId = $t['roomId'];
					array_push($wall_textures, $texture);
				}
			}	
			if($r['facilities']!="null"){
			foreach($r['facilities'] as $f) {
				$ofacilities = new stdClass();
				$fs = new Facilities;
					$geo = GoodsGeometry::model() -> findById($f['gid']['$id']);
					$fs -> gid = $geo[0];
					// $fs -> rndId = $f['rndId'];
					$fs -> position = $f['position'];
					$fs -> rotation = $f['rotation'];
					$fs -> parasitizedFace = $f['parasitizedFace'];
					// $fs -> gTypeId = $geo['gTypeId'];
					// $fs -> name = $geo['goodsName'];
					// $fs -> description = $f['description'];
					// $fs -> roomId = $r['roomId'];
					
				$ofacilities->roomId = $r['roomId'];
				$ofacilities->fs=$fs;
				array_push($faciStyle, $ofacilities);
			}
			}
		}
		$object -> faciStyle = $faciStyle;
		$object -> wallTextures = $wall_textures;
		$object -> _id = $h['_id'];
		echo json_encode($object);


	}

	public function actionShowHouse($uid = 1) {
		$house = Houses::model() -> findAllByUid($uid);
		// echo json_encode($house);
		$this -> render('showHouse', array('house' => $house));

		// $this->renderPartial ( 'showHouse');
	}

	public function actionShowHouseStyle($hsid = "111100") {
		$hs = HouseStyle::model() -> findByHouseType($hsid);
		// echo json_encode($hs);
		$this -> renderPartial('showHouseStyle', array('hs' => $hs));

	}

	public function actionLoadGeometry($gTypeId) {

	}
	
	public function actionTest(){
		$this -> layout = 'application.views.layouts.layout0429';
		$this -> render('test');
	}

}
